package com.xinfu.newproject.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.activity.ChooseCommunityActivity;
import com.xinfu.newproject.activity.HomeActivity;
import com.xinfu.newproject.activity.NoticeActivity;
import com.xinfu.newproject.adapter.home_provider.HomeMultipleItemRvAdapter;
import com.xinfu.newproject.base.BaseFragment;
import com.xinfu.newproject.bean.home.bean.GetAppIconBean;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.contants.Contants;
import com.xinfu.newproject.data.DataServer;
import com.xinfu.newproject.entity.MultipleItem;
import com.xinfu.newproject.entity.NormalMultipleEntity;
import com.xinfu.newproject.presenter.HomePresenter;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.SPUtils;
import com.xinfu.newproject.view.IView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.disposables.Disposable;

import static android.app.Activity.RESULT_OK;
import static com.xinfu.newproject.contants.Constant.COMMUNITY;
import static com.xinfu.newproject.contants.Constant.LOGIN;

public class HomeFragment extends BaseFragment implements IView.IAppIconView {

    private List<NormalMultipleEntity> mData;

    @BindView(R.id.fragment_home_notice_iv)
    ImageView noticeIv;

    @BindView(R.id.fragment_home_location_tv)
    TextView locationTv;

    private HomePresenter homePresenter = new HomePresenter(this);

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_home;
    }

    RecyclerView mRecyclerView;
    @Override
    protected void setUpView() {
//        setBackBtn();

        mRecyclerView = getContentView().findViewById(R.id.fragment_home_rv);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getMContext()));
    }

    @Override
    protected void setUpData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        refresh();
    }

    @OnClick({R.id.fragment_home_notice_iv, R.id.fragment_home_location_tv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fragment_home_notice_iv:
                if (isLogin()) {
                    ((HomeActivity) getMContext()).startIntentActivity(NoticeActivity.class, null);
                }
                break;
            case R.id.fragment_home_location_tv:
//                ((HomeActivity) getMContext()).startIntentActivityForResult(ChooseCommunityActivity.class,null, Contants.CHOOSECOMMUNITYDATA);
                if (isLogin()) {
                    startActivityForResult(new Intent(getMContext(), ChooseCommunityActivity.class), Contants.CHOOSECOMMUNITYDATA);
                }
                break;
        }
    }

    private void refresh() {
        if (SPUtils.get(getMContext(), COMMUNITY, "0").equals("0")) {
            locationTv.setText("正在定位中");
        } else {
            String response = (String) SPUtils.get(getMContext(), COMMUNITY, "0");
            Log.i("testHome", response);
//            GetCommunityBean.ResultBean resultBean = new Gson().fromJson(response, GetCommunityBean.ResultBean.class);
            locationTv.setText(response);
        }

        if (isLogin()) {
            homePresenter.appIcon();
        } else {
            userId = "";
            homePresenter.appIcon();
        }
    }

    /**
     * 判断是否可以请求数据
     */
    String token, userId, userName;

    private boolean isLogin() {

        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(getMContext(), LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
        } else {
            BToast.showText(getMContext(), "请先登录");
            return false;
        }

        return true;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("testTwo", "requestCode=" + requestCode + "resultCode=" + resultCode);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case Contants.CHOOSECOMMUNITYDATA:
                    if (data.getStringExtra("name") != null) {
                        locationTv.setText(data.getStringExtra("name"));
                    }
                    break;
            }
        }
    }

    @Override
    public String header() {
        return token;
    }

    @Override
    public String userId() {
        return userId;
    }

    @Override
    public void showToast(String content) {
        BToast.showText(getMContext(), content);
    }

    @Override
    public void closeDispose(Disposable disposable) {

    }

    @Override
    public void showProgress() {
        ((HomeActivity) getMContext()).showLoding();
    }

    @Override
    public void hideProgress() {
        ((HomeActivity) getMContext()).hideLoding();
    }

    @Override
    public void getData(GetAppIconBean getAppIconBean) {
        mData = DataServer.getNormalMultipleEntities();
        HomeMultipleItemRvAdapter multipleItemAdapter = new HomeMultipleItemRvAdapter(mData, getAppIconBean);

        multipleItemAdapter.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
            @Override
            public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                int type = mData.get(position).type;
                if (type == NormalMultipleEntity.SINGLE_One) {
                    return MultipleItem.TEXT_SPAN_SIZE;
                } else if (type == NormalMultipleEntity.SINGLE_Two) {
                    return MultipleItem.IMG_SPAN_SIZE;
                } else {
                    return MultipleItem.IMG_TEXT_SPAN_SIZE;
                }
            }
        });
        if (mRecyclerView != null) {
            mRecyclerView.setAdapter(multipleItemAdapter);
        }

    }
}
